package com.lzx.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.activity.entity.Activity;
import com.lzx.activity.entity.Voucher;
import com.lzx.activity.entity.vo.ActivityOSSParams;
import com.lzx.activity.entity.vo.ActivityParams;
import com.lzx.activity.mapper.ActivityMapper;
import com.lzx.activity.mapper.VoucherMapper;
import com.lzx.activity.service.ActivityService;
import com.lzx.common.utils.ExcelUtils;
import com.lzx.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2022-10-31
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {
    @Resource
    private ActivityMapper activityMapper;

    @Resource
    private VoucherMapper voucherMapper;

    //获取活动列表
    @Override
    public R getActivityList(int current, int size) {
        Page<Activity> page = new Page<>(current, size);
        activityMapper.selectPage(page, null);
        Map<String, Object> map = new HashMap<>();
        List<Activity> list = page.getRecords();//当前页数据
        long total = page.getTotal();//总记录数
        map.put("list", list);
        map.put("total", total);
        return R.ok().data("map", map);
    }

    //新增活动
    @Override
    public R addActivity(Activity activity) {
        activityMapper.insert(activity);
        return R.ok().message("新增成功");
    }

    //修改活动
    @Override
    public R updateActivity(Activity activity) {
        activityMapper.updateById(activity);
        return R.ok().message("修改成功");
    }

    //删除活动
    @Override
    public R deleteActivity(List<Long> ids) {
        for (Long id : ids) {
            Activity activity = activityMapper.selectById(id);
            activity.setStatus(1);
            activityMapper.updateById(activity);
        }
        return R.ok().message("删除成功!");
    }

    //根据id获取活动信息
    @Override
    public R getActivity(Long id) {
        Activity activity = activityMapper.selectById(id);
        return R.ok().data("activity", activity);
    }

    //搜索
    @Override
    public R searchActivity(ActivityParams activityParams) {
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNoneBlank(activityParams.getName())) {
            wrapper.like("name", activityParams.getName());
        }
        if (StringUtils.isNoneBlank(activityParams.getIntroduction())) {
            wrapper.like("introduction", activityParams.getIntroduction());
        }
        if (StringUtils.isNoneBlank(activityParams.getPublisher())) {
            wrapper.like("publisher", activityParams.getPublisher());
        }
        if (activityParams.getStatus() != null) {
            wrapper.eq("status", activityParams.getStatus());
        }
        List<Activity> list = activityMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }

    //导出
    @Override
    public void exportActivity(HttpServletResponse response) {
        List<Activity> list = activityMapper.selectList(null);
        ExcelUtils.export(response, "活动信息", Activity.class, list);
    }

    //根据id获取活动图片
    @Override
    public String getImageAndUpdateImage(ActivityOSSParams activityOSSParams) {
        Activity activity = activityMapper.selectById(activityOSSParams.getId());
        if (StringUtils.isNoneBlank(activity.getImage())) {
            return activity.getImage();
        } else {
            activity.setImage(activityOSSParams.getImage());
            activityMapper.updateById(activity);
            return null;
        }
    }

    //获取活动列表
    @Override
    public R getList() {
        QueryWrapper<Activity> wrapper = new QueryWrapper<>();
        wrapper.eq("status", 0);
        List<Activity> list = activityMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }

    //根据活动id查询活动和优惠券
    @Override
    public R getActivityAndVoucher(Long id) {
        Map<String, Object> map = new HashMap<>();
        Activity activity = activityMapper.selectById(id);
        QueryWrapper<Voucher> wrapper = new QueryWrapper<>();
        wrapper.eq("activity_id", id).eq("status", 0);
        List<Voucher> voucherList = voucherMapper.selectList(wrapper);
        map.put("activity", activity);
        map.put("voucherList", voucherList);
        return R.ok().data("map", map);
    }
}
